<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Resume extends Model
{
    const UPDATED_AT = null;
    const CREATED_AT = null;
    public $table = 'resume';
    protected $fillable = [
        'user_id', 'nickname', 'telephone', 'refresh_time', 'create_time'
    ];

    public function getBirthCnAttribute()
    {
        return date('Y') - date('Y', $this->birth);
    }

    /**
     * 计算完整度
     * @param Resume $resume
     * @return int
     */
    public static function getPress(Resume $resume)
    {
        $press = 10;
        if (isSet($resume->nickname) && !empty($resume->nickname)) {
            $press = $press + 45;
        }
        if (isSet($resume->current) && !empty($resume->current)) {
            $press = $press + 25;
        }
        if (isSet($resume->specialty) && !empty($resume->specialty)) {
            $press = $press + 20;
        }
        return $press;
    }

    public static function search($params)
    {

        return Resume::paginate(10);
    }

    public static function searchResult($category, $params)
    {
        DB::connection()->enableQueryLog();  // 开启QueryLog
        $result = Resume::where(function ($query) use ($params) {
            if (isSet($params['key'])) {
                $query->where('nickname', 'like', '%' . $params['key'] . '%')
                    ->orWhere('trade_cn', 'like', '%' . $params['key'] . '%');
            }
        })->where(function ($query) use ($params, $category) {
            if (isSet($params['wage'])) {
                $query->where('wage', (int)$params['wage']);
            }
            if (isSet($params['experience']) && $params['experience']) {
                $query->where('experience', (int)$params['experience']);
            }
            if (isSet($params['education'])) {
                $query->where('education', (int)$params['education']);
            }
            if (isSet($params['sex'])) {
                $query->where('sex', (int)$params['sex']);
            }
            if (isSet($params['age']) && $params['age']) {
                //获取区间
                $category_cn = Category::getCategoryCn($category, '_age', $params['age']);
                $section = explode("-", $category_cn);
                if (isSet($section[1])) {
                    $star = preg_replace('/[^\w]+/', '', $section[1]);
                    $query->where('birth', '>', date("Y") - $star);
                }
                if (isSet($section[0])) {
                    $end = preg_replace('/[^\w]+/', '', $section[0]);
                    $query->where('birth', '<', date("Y") - $end);
                }
            }
        })->where(function ($query) use ($params, $category) {
            if (isSet($params['trade'])) {
                $trade = Category::select('id')->where('pid', $params['trade'])->where('alias', '_trade')->get()->toArray();
                $trade = array_column($trade, 'id');
                foreach ($trade as $item) {
                    $query->orwhere('intention', 'like', '%' . $item . '%');
                }
            }
        })->where('nickname', '!=', '');
        if (isSet($params['sort'])) {
            if ($params['sort'] == '1') {
                //相关度排序
                $result = $result->orderBy('refresh_time', 'desc');
            } else {
                //$result = $result->orderBy('refresh_time', 'asc');
            }
        }
        $result = $result->paginate(10);
        return $result;
    }
}
